import undetected_chromedriver as uc
from lxml import etree
from selenium.webdriver.common.by import By
from dbse.mysql.db_mysql_insert import addStockNews
from dbse.mysql.db_entity_stock_news import StockNews
from dbse.mysql.db_mysql_insert import addThsReportInform
from dbse.mysql.db_entity_stock_ths_report_inform import StockThsReportInform
from selenium.webdriver import ChromeOptions
import time
import pymysql
from file.readFile import read

#
def sel_report_page(browser,db,page,sql):
    content = browser.page_source
    html = etree.fromstring(content,etree.HTMLParser())
    result = html.xpath('//tbody//tr')      # tbody 下的每一行数据
    for index, tr in enumerate(result):
        tds = tr.getchildren()
        print(f"====index:{index}")
        print(tds[1].find('a').text)    # 股票代码
        print(tds[2].find('a').text)    # 股票名称
        print(tds[3].text)    #公告日期
        print(tds[4].text)  #营业收入
        print(tds[5].text)  # 同比增长
        print(tds[6].text)  # 季度环比增长
        print(tds[7].text)  # 净利润
        print(tds[8].text)  # 同比增长
        print(tds[9].text)  # 季度环比增长
        print(tds[10].text) # 每股收益
        print(tds[11].text) # 每股净资产
        print(tds[12].text) # 净资产收益率
        print(tds[13].text) # 每股经营现金流量
        print(tds[14].text) # 销售毛利率
        entity = StockThsReportInform(tds[1].find('a').text,
                                      tds[2].find('a').text,
                                      tds[3].text,
                                      tds[4].text,
                                      tds[5].text,
                                      tds[6].text,
                                      tds[7].text,
                                      tds[8].text,
                                      tds[9].text,
                                      tds[10].text,
                                      tds[11].text,
                                      tds[12].text,
                                      tds[13].text,
                                      tds[14].text)

        addThsReportInform(db,sql,entity)




driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
option = ChromeOptions()
option.add_argument('--headless')
# browser = uc.Chrome(driver_executable_path=driver_path,options=option)
browser = uc.Chrome(driver_executable_path=driver_path)
browser.set_window_size(1366,768)
browser.get('https://data.10jqka.com.cn/financial/yjgg')
print('====begin====')
content = browser.page_source
# content = read('../file/text_ths_report_inform.txt')    # 财报
html = etree.fromstring(content,etree.HTMLParser())
result = html.xpath('//tbody//tr')
# print(result)
"""

"""
# 打印所有 tbody 下 tr 所有属性
db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
sql = 'INSERT INTO stock_ths_report_inform (stock_code, stock_name,inform_date,yysr,yysrtbzz,yysrjdhbzz,jlr,jlrtbzz,jlrjdhbzz,mgsy,mgjzc,jzcsyl,mgjyxjll,xsmll) VALUES (%s, %s,%s,%s,%s, %s,%s,%s,%s, %s,%s,%s,%s, %s)'
for index, tr in enumerate(result):
    tds = tr.getchildren()
    print(f"====index:{index}")
    print(tds[1].find('a').text)    # 股票代码
    print(tds[2].find('a').text)    # 股票名称
    print(tds[3].text)    #公告日期
    print(tds[4].text)  #营业收入
    print(tds[5].text)  # 同比增长
    print(tds[6].text)  # 季度环比增长
    print(tds[7].text)  # 净利润
    print(tds[8].text)  # 同比增长
    print(tds[9].text)  # 季度环比增长
    print(tds[10].text) # 每股收益
    print(tds[11].text) # 每股净资产
    print(tds[12].text) # 净资产收益率
    print(tds[13].text) # 每股经营现金流量
    print(tds[14].text) # 销售毛利率
    entity = StockThsReportInform(tds[1].find('a').text,
                                    tds[2].find('a').text,
                                    tds[3].text,
                                    tds[4].text,
                                    tds[5].text,
                                    tds[6].text,
                                    tds[7].text,
                                    tds[8].text,
                                    tds[9].text,
                                    tds[10].text,
                                    tds[11].text,
                                    tds[12].text,
                                    tds[13].text,
                                    tds[14].text)


    addThsReportInform(db,sql,entity)
    #
    # for td in tds:
    #     # print(tr.attrib)
    #     # tds = tr.getchildren();
    #     # print(tds)
    #     print(td.text)
# browser.find_element 取第一个元素  find_elements 返回的是数组
nextPage = browser.find_element(By.XPATH,"//a[@page='2']")
nextPage.click()
time.sleep(2)

# ===== 打印第二页
content = browser.page_source
html = etree.fromstring(content,etree.HTMLParser())
result = html.xpath('//tbody//tr')
for index, tr in enumerate(result):
    tds = tr.getchildren()
    print(f"====index:{index}")
    print(tds[1].find('a').text)    # 股票代码
    print(tds[2].find('a').text)    # 股票名称
    print(tds[3].text)    #公告日期
    print(tds[4].text)  #营业收入
    print(tds[5].text)  # 同比增长
    print(tds[6].text)  # 季度环比增长
    print(tds[7].text)  # 净利润
    print(tds[8].text)  # 同比增长
    print(tds[9].text)  # 季度环比增长
    print(tds[10].text) # 每股收益
    print(tds[11].text) # 每股净资产
    print(tds[12].text) # 净资产收益率
    print(tds[13].text) # 每股经营现金流量
    print(tds[14].text) # 销售毛利率
    entity = StockThsReportInform(tds[1].find('a').text,
                                  tds[2].find('a').text,
                                  tds[3].text,
                                  tds[4].text,
                                  tds[5].text,
                                  tds[6].text,
                                  tds[7].text,
                                  tds[8].text,
                                  tds[9].text,
                                  tds[10].text,
                                  tds[11].text,
                                  tds[12].text,
                                  tds[13].text,
                                  tds[14].text)


    addThsReportInform(db,sql,entity)


















# last_total = 0
# while True:
#     time.sleep(60)
#     print("======================get news==========================")
#     html = etree.fromstring(browser.page_source,etree.HTMLParser())
#     urls = html.xpath('//div[contains(@class,"share-popup")]//@data-url')
#     total = len(urls)
#     add_num = total - last_total
#     last_total = total
#     # for item in urls:
#     #     print(item)
#
#     titles = html.xpath('//div[contains(@class,"share-popup")]//@data-title')
#     # for item in titles:
#     #     print(item)
#
#     # 多个class 选择器
#     descs = html.xpath('//div[contains(@class,"share-popup")]//@data-description')
#     # for item in descs:
#     #     print(item)
#
#     newsTimes = html.xpath('//ul//div[@class="time"]//text()')
#     # for item in newsTimes:
#     #     print(item)
#
#     # print("================================================")
#     # print(urls[0])
#
#     if add_num > 0:
#         print("======================news insert db==========================")
#         db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
#         sql = 'INSERT INTO stock_news (title, content,channel,url) VALUES (%s, %s,%s,%s)'
#         # for index,item in enumerate(urls):
#         #     print(titles[index])
#         #     stockNews = StockNews(titles[index],descs[index],'e网',urls[index])
#         #     addStockNews(db,sql,stockNews)
#         for index in range(add_num):
#             stockNews = StockNews(titles[index],descs[index],'e网',urls[index])
#             addStockNews(db,sql,stockNews)
#         db.close()





